﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Data.UserDb;
using System.Text;

namespace RoomWeb
{
    public partial class roomInfo : System.Web.UI.Page
    {
        private const string TemplateStr = "<tr>" +
                                           "<td height=\"35\" class=\"border_bottom1\" align=\"center\"><img src=\"images/no{0}.gif\" />{1}</td>" +
                                           "<td class=\"border_bottom1\" align=\"center\"><span class=\"font_bold color_blue\">{2}</span></td>" +
                                           "<td class=\"border_bottom1\" align=\"center\">{3}</td>" +
                                           "<td class=\"border_bottom1\" align=\"center\">{4}</td>" +
                                           "<td class=\"border_bottom1\" align=\"center\"><a href=\"{5}\"><img src=\"images/play_banner.gif\" /></a></td>" +
                                           "</tr>";
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                String reqRoomId = Request.QueryString["roomid"];
                if(String.IsNullOrEmpty(reqRoomId)) return;

                var roomInfo = Media.GetRoomById(int.Parse(reqRoomId));
                if (roomInfo == null) return;

                literalRoomName.Text = roomInfo.Roomname;
                literalRoomId.Text = roomInfo.Roomid.ToString();
                literalExp.Text = roomInfo.Exp.ToString();
                literalExpRate.Text = roomInfo.Exprate.ToString();
                literalAdminName.Text = roomInfo.Mngername;
                literalHeader.Text = roomInfo.Mngerheader;
                summary.Text = string.Format("{0}影院由{1}创办。", roomInfo.Roomname, roomInfo.Mngername);
                detail.Text = roomInfo.Roomdesc;

                var param = String.Format("<a href='javascript:;' onclick=\"return joinRoom({0}, {1}, '{2}', {3});\"><img src=\"images/yy_banner.gif\" /></a>", roomInfo.Chnlid, roomInfo.Roomid,
                                                 roomInfo.Roomipaddr, roomInfo.Roomipaddr.Length);
                joinRoom.Text = param;


                var progs = Media.GetProgs(int.Parse(reqRoomId));
                if (progs.Count() == 0) return;

                var sb = new StringBuilder();
                var i = 1;
                foreach (var prog in progs)
                {
                    
                    sb.AppendFormat(TemplateStr, i++, prog.TheatersName, prog.Name, GTicket(prog.Ticketid), prog.Times,
                                    prog.Url);
                }
                _infos.Text = sb.ToString();
            }
        }


        private static String GTicket(int t)
        {
            String tmp = "";
            for (int i = 0, t1 =1; i < 5; i++, t1 <<= 1)
            {
                if ((t & t1) != 0)
                    tmp += String.Format("<img src=\"images/p{0}.gif\" border='0'/>", i + 1);

            }
            return tmp;
        }
    }
}
